package com.example.flashsales.service.impl;

import com.example.flashsales.entity.dto.AdminDto;
import com.example.flashsales.entity.pojo.Admin;
import com.example.flashsales.entity.pojo.Method;
import com.example.flashsales.entity.pojo.Role;
import com.example.flashsales.entity.vo.AdminVo;
import com.example.flashsales.mappers.RoleMapper;
import com.example.flashsales.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * 查看权限
     * @return
     */
    @Override
    public List<Integer> viewRole(int adminId) {
        return roleMapper.viewRole(adminId);
    }

    /**
     * 查看全部权限
     * @return
     */
    @Override
    public List<Role> viewAllRole() {
        return roleMapper.viewAllRole();
    }

    /**
     * 登录
     * @param adminDto
     * @return
     */
    @Override
    public Admin login(AdminDto adminDto) {
        return roleMapper.login(adminDto);
    }

    /**
     * 注册管理人员
     * @param adminDto
     * @return
     */
    @Override
    @Transactional
    public boolean registerAdmin(AdminDto adminDto) {
        if (roleMapper.register(adminDto) > 0) {
            //在admin_role里面添加数据
            if (roleMapper.addAdminRole(adminDto.getId(),adminDto.getState()) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据账号查询后台管理人员
     * @param account
     * @return
     */
    @Override
    public Admin viewAdminByAccount(String account) {
        return roleMapper.viewAdminByAccount(account);
    }

    /**
     * 根据id查询信息
     * @param adminId
     * @return
     */
    @Override
    public Admin viewAdminById(Integer adminId) {
        return roleMapper.viewAdminById(adminId);
    }

    /**
     * 修改后台人员状态
     * @param adminId
     * @param i
     * @return
     */
    @Override
    public Boolean updateAdminState(Integer adminId, int i) {
        return roleMapper.updateAdminState(adminId,i) > 0;
    }

    /**
     * 查看所有后台人员
     * @param page
     * @param num
     * @param adminId
     * @param phone
     * @param s 状态
     * @param state 身份
     * @return
     */
    @Override
    public List<AdminVo> viewAllAdmin(Integer page, Integer num, Integer adminId, String phone,Integer s,Integer state) {
        PageHelper.startPage(page,num);
        return roleMapper.viewAllAdmin(adminId,phone,s,state);
    }

    /**
     * 查看所有方法
     * @return
     */
    @Override
    public List<Method> viewAllMethod() {
        return roleMapper.viewAllMethod();
    }

    /**
     * 根据角色查看方法
     * @param state
     * @return
     */
    @Override
    public List<Method> viewMethodByState(Integer state) {
        return roleMapper.viewMethodByState(state);
    }

    /**
     * 查看管理人员拥有的方法
     * @param adminId
     * @return
     */
    @Override
    public List<Method> viewMethodByAdmin(Integer adminId) {
        final Admin admin = viewAdminById(adminId);
        final List<Method> methods = viewMethodByState(admin.getState());
        //去admin_method里面查看拥有的方法和没有的方法
        final List<Method> methodsNo = roleMapper.viewNoYesMethod(adminId, 1);
        final List<Method> methodsYes = roleMapper.viewNoYesMethod(adminId, 0);
        for (Method method : methodsNo) {
            if (methods.contains(method)){
                methods.remove(method);
            }
        }
        for (Method methodsYe : methodsYes) {
            if (!methods.contains(methodsYe)){
                methods.add(methodsYe);
            }
        }
        return methods;
    }

    /**
     * 查看没有的方法
     * @param adminId
     * @return
     */
    @Override
    public List<Method> viewMethodByAdminNo(Integer adminId) {
        final List<Method> methods = viewAllMethod();
        final List<Method> methods1 = viewMethodByAdmin(adminId);
        for (Method method : methods1) {
            if(methods.contains(method)){
                methods.remove(method);
            }
        }
        return methods;
    }

    /**
     * 管理员 方法
     * @param adminId
     * @param methodId
     * @param i
     * @return
     */
    @Override
    public Boolean addMethodToAdmin(Integer adminId, Integer methodId, int i) {
        final Method method = roleMapper.viewMethodByMethodIdAndAdminId(adminId, methodId);
        System.out.println(method);
        if (method == null){
            return roleMapper.addMethodToAdmin(adminId,methodId,i) > 0;
        }else {
            return roleMapper.updateMethodState(adminId,methodId,i) > 0;
        }
    }


}
